package soa.security.support;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import soa.security.mgr.service.AppService;
import v1.web.project.WebProjectSupport;

/**
 * Web容器监听器，提供了容器运行生命周期的扩展
 * 
 * 
 * @author wangs
 * 
 */
public class ProjectBootstrap implements ServletContextListener {
	protected static final org.slf4j.Logger log = org.slf4j.LoggerFactory
			.getLogger(ProjectBootstrap.class);

	public void contextDestroyed(ServletContextEvent arg0) {
		log.info("系统关闭");

	}

	public void contextInitialized(ServletContextEvent e) {
		ServletContext servletContext = e.getServletContext();

		// 保存项目基本环境信息
		String root = servletContext.getRealPath("/");
		log.info("系统启动成功，Webapp路径：{}", root);

		// 构建配置环境
		WebApplicationContext applicationContext = WebApplicationContextUtils
				.getWebApplicationContext(servletContext);

		WebProjectSupport.newInstance(
				(ConfigurableEnvironment) applicationContext.getEnvironment())
				.putProperty(WebProjectSupport.PATH_ROOT, root);
		if (log.isDebugEnabled()) {
			log.debug(WebProjectSupport.getInstance().toString());
		}

		// 初始化项目数据
		AppService service = applicationContext
				.getBean(AppService.class);
		log.debug("...项目数据初始化");
		service.initData();
	}
}
